import log from 'log-tips'

function identify(value) {
  return value
}

function handleAction(actionType, reducer = identify) {
  return (state, action) => {
    const { type } = action
    log(type, 'dispatch: action should be a plain Object with type')
    if (actionType === type) {
      return reducer(state, action)
    }
    return state
  }
}

function reduceReducers(...reducers) {
  return (previous, current) => reducers.reduce((p, r) => r(p, current), previous)
}

function handleActions(handlers, defaultState) {
  const reducers = Object.keys(handlers).map(type => handleAction(type, handlers[type]))
  const reducer = reduceReducers(...reducers)
  return (state = defaultState, action) => reducer(state, action)
}

export default handleActions
